package com.servlet;

import com.pojo.User;
import com.service.apply.ApplyServiceImpl;
import com.service.good.GoodServiceImpl;
import com.util.Constant;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GoodAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object o = req.getSession().getAttribute(Constant.USER_SESSION);

        int userId = ((User)o).getUserId();

        String goodName = req.getParameter("goodName");
        String goodExplain = req.getParameter("goodExplain");
        String goodAddress = req.getParameter("goodAddress");
        String msg = "";
        GoodServiceImpl goodService = new GoodServiceImpl();
        int flag = goodService.GoodAdd(userId, goodName, goodAddress, goodExplain);
        if(flag > 0){
            msg = "发布成功";
            req.getRequestDispatcher("/jsp/allgoods.jsp").forward(req,resp);
        }else{
            msg = "发布失败";
            req.getRequestDispatcher("/jsp/addgood.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
